//
//  FreeAdoptedPigsViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "FreeAdoptedPigsViewController.h"
#import "AdoptedCell.h"
#import "AdoptedSelectTrusteeViewController.h"
#import "LookViewController.h"

@interface FreeAdoptedPigsViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 currentPage
 */
@property (nonatomic , assign) int currentPage;
/**
 limit
 */
@property (nonatomic , assign) int limit;

@end

@implementation FreeAdoptedPigsViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self.dataArr removeAllObjects];
        [self dataRequest];
    }];
    [self.myTableView.mj_header beginRefreshing];
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self dataRequest];
    }];
    self.myTableView.mj_footer.hidden = YES;
}

- (void)initData {
    self.currentPage = 1;
    self.limit = 10;
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AdoptedCell *cell = [AdoptedCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        Adopted *model = self.dataArr[indexPath.row];
        [cell configCellWithModel:model adoptedType:AdoptedTypeFree];
        @weakify(self);
        
        [cell setLookBlock:^{
            @strongify(self);
            LookViewController *vc = [[LookViewController alloc] init];
            vc.pigId = @([model.pigId integerValue]);
            [self.navigationController pushViewController:vc animated:YES];

        }];
        [cell setPayBlock:^{
            @strongify(self);
            AdoptedSelectTrusteeViewController *vc = [[AdoptedSelectTrusteeViewController alloc] init];
            vc.adoptPlanId = model.adoptPlanId;
            vc.adoptType = AdoptedTypeFree;
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - target action

#pragma mark - private method
- (void)dataRequest {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    AdoptedParam *param = [[AdoptedParam alloc] init];
    param.memberId = memberId;
    param.limit = @(self.limit);
    param.page = @(self.currentPage);
    param.type = @(AdoptedTypeFree);
    @weakify(self);
    [[[PigMarketApi sharedInstance] AdoptedListWithParam:param] subscribeNext:^(AdoptedList * _Nullable adoptedList) {
        @strongify(self);
        [self.dataArr addObjectsFromArray:adoptedList.list];
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        if (self.currentPage == [adoptedList.totalPage intValue] || [adoptedList.totalPage intValue] == 0) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];;
        } else {
            self.myTableView.mj_footer.hidden = NO;
        }
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 242;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end
